package com.lgj.springboot_lgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.BaseConfig;
import com.lgj.springboot_lgj.dao.basic.BaseConfigMapper;
import com.lgj.springboot_lgj.dao.ext.BaseConfigExtMapper;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.IBaseConfigService;
import com.lgj.springboot_lgj.utils.ResultStatus;
import com.lgj.springboot_lgj.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:Lijun
 * @Date:2022/6/19
 */
@Service
public class BaseConfigServiceImpl implements IBaseConfigService {
    @Autowired
    private BaseConfigMapper configMapper;
    @Autowired
    private BaseConfigExtMapper extMapper;
    @Override
    //保存配置
    public void saveConfig(BaseConfig baseConfig) {
        baseConfig.setConfigStatus(0);//默认设置为0
        if(extMapper.selectConfigByName(baseConfig.getConfigName())!=null){
            throw new CustomerException(baseConfig.getConfigName()+"配置名称已存在！保存失败");
        }
        configMapper.insert(baseConfig);
    }
    //通过id更新
    @Override
    public void updateConfig(BaseConfig baseConfig) {
        if(configMapper.selectByPrimaryKey(baseConfig.getConfigId())==null){
            throw new CustomerException("没有该id的配置信息,请重新输入");
        }
        if(extMapper.selectConfigByName(baseConfig.getConfigName())!=null&&extMapper.selectConfigByName(baseConfig.getConfigName()).getConfigId()!=baseConfig.getConfigId()){
            throw new CustomerException(baseConfig.getConfigName()+"配置名称已存在！修改失败");
        }
        extMapper.updateConfig(baseConfig);
    }
    //改变当前状态
    @Override
    public void changeConfigStatus(int id, int status) {

        if(configMapper.selectByPrimaryKey(id)==null){
            throw new CustomerException("没有该id的配置信息，请重新输入");
        }
        if(status!=0&&status!=1){
            throw new CustomerException("状态只能为0或1，请重新输入！");
        }
        //1.先把status为1的改为0
        //2.在执行修改动作
        if(status==1&&extMapper.selectConfigByStatus()!=null){
            Integer id2 = extMapper.selectConfigByStatus().getConfigId();
            extMapper.updateConfigStatusById(id2,0);
        }
        if(status==0&&extMapper.selectConfigByStatus().getConfigId()==id){
           throw new CustomerException("禁用失败，必须保留一个启动配置");
        }
        extMapper.updateConfigStatusById(id,status);
    }
    //删除
    @Override
    public void deleteById(int id) {
        //没有数据
        if(configMapper.selectByPrimaryKey(id)==null){
            throw new CustomerException("没有该id的配置信息，请重新输入");
        }
        //只有一个不能删除
        if(extMapper.selectConfigAll().size()==1){
            throw new CustomerException(ResultStatus.SPECIFIED_QUESTIONED_ONLYONE_FORBID_DEL);
        }
        //不能删除开放的信息
        if(  configMapper.selectByPrimaryKey(id).getConfigStatus()==1){
            throw new CustomerException(ResultStatus.SPECIFIED_QUESTIONED_OPEN_FORBID_DEL);
        }
        configMapper.deleteByPrimaryKey(id);
    }
    //分页查询配置信息
    @Override
    public PageInfo<BaseConfig> findAllWithPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<BaseConfig> list = extMapper.selectConfigAll();
        PageInfo<BaseConfig> pageInfo = new PageInfo<>(list);
        if(pageInfo.getList().size()==0){
            throw new CustomerException("查询失败，没有数据，请重试");
        }
        return pageInfo;
    }
    //查询当前开放的接口
    @Override
    public BaseConfig currentConfigYes() {
        BaseConfig baseConfig = extMapper.selectConfigByStatus();
        return baseConfig;
    }
}
